package com.example.jdcompetition.Service.cvsPro.jd;

import com.example.jdcompetition.Service.cvsPro.time.CollectTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;

import static com.example.jdcompetition.JdcompetitionApplication.collect;
import static com.example.jdcompetition.Service.cvsPro.dataprocess.CsvProcess.oneDay;

/**
 * @author henrydai
 * @date 2021/6/19 15:55
 */
@Service
@Slf4j
public class ExeCollect {

    public long execute(String[] ori,String[] des,long orderDate,long orderTime)
    {
        int x=4,y=2; //4-2级
        String OriginateAddress = ori[0]+ori[1]+ori[2]+ori[3];
        String DestinationAddress = des[0]+des[1];
        while(x>0)
        {
            //2:根据地址码查map
            HashMap<String, LinkedList<CollectTime>> collectMap = collect.get(OriginateAddress);
            if(collectMap!=null)
            {
                //3:根据目的省市查揽收时间
                LinkedList<CollectTime> collectList = collectMap.get(DestinationAddress );
                if(collectList!=null)
                {
                    //查询成功
                    //安全起见:排一次序(如果查询时间过长可删)
                    Collections.sort(collectList);
                    CollectTime ct = getCollectTime(orderDate,collectList);
                    if(ct!=null)
                    {
                        //成功则返回
                        //当天订单揽收超过deadline,中转时间增加1天
                        if(orderTime+ct.collectTime>ct.collectDeadline)
                        {
                            return oneDay; //一天增量
                        }
                        return 0; //本次揽收没有增量
                    }
                }
            }
            else
            {
                y=0;
            }

            //若4-2级查询失败循环降级查询 可以有4-0
            if(y>0)
                y--;
            else
            {
                y=2;
                x--;
            }
            if(x<=0)
            {
                log.info("没有合适的揽收");
                return -1;
            }
            OriginateAddress="";
            DestinationAddress="";
            for(int i=0;i<x;i++)
            {
                OriginateAddress+=ori[i];
            }
            for(int i=0;i<y;i++)
            {
                DestinationAddress+=des[i];
            }
        }
        return -1;
    }

    private CollectTime getCollectTime(long orderDate, LinkedList<CollectTime> collectList)
    {
        int low=0;
        int high=collectList.size()-1;
        int mid;
        while(low<high)
        {
            mid=(low+high+1)/2;
            if(collectList.get(mid).effectiveDate>orderDate)
            {
                high=mid-1;
            }
            else
            {
                low=mid;
            }
        }
        CollectTime temp = collectList.get(low);
        //考虑当天揽收时间

        if(temp.effectiveDate>orderDate || orderDate>temp.expirationDate)
            return null;
        else
            return temp;
    }
}
